package com.zch.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.base.dialect.PaginationSupport;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import com.item.service.MessageService;

import org.springframework.beans.factory.annotation.Autowired;
import com.base.action.CoreController;

import com.zch.dao.model.ProjectMessage;
import com.zch.dao.model.ProjectMessageExample;
import com.zch.dao.model.ZaitiType;
import com.zch.daoEx.model.ProjectMessageEx;
import com.zch.daoEx.model.ZxMessageEx;
import com.zch.service.ProjectMessageService;

@RequestMapping("projectMessage")
@Controller
@LoginFilter
public class ProjectMessageController extends CoreController{

	@Autowired
	private ProjectMessageService projectMessageService;
	@Autowired
    private MessageService messageService;
	@RequestMapping("/list")
	@ResponseBody
	public String list(Integer page, Integer rows, String name){
        PaginationSupport.byPage(page, rows);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", name);
        List<ProjectMessageEx> list = projectMessageService.selectList(map);
        ProjectMessageEx projectMessageEx = new  ProjectMessageEx ();
        String names = projectMessageEx.getCyTypeName();
        projectMessageEx.setCyTypeId(projectMessageEx.getCyTypeName());

        return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(ProjectMessage projectMessage){
		if (StringUtils.isBlank(projectMessage.getId())){
			projectMessageService.insert(projectMessage);
		}else{
			projectMessageService.updateByPrimaryKeySelective(projectMessage);
			
			projectMessage = projectMessageService.selectByPrimaryKey(projectMessage.getId());
            String pushContent = "";
            if (projectMessage.getIsAgree() == 2 && projectMessage.getNotAgreeReason()!=null){
                pushContent = "审核未通过!原因"+projectMessage.getNotAgreeReason();
                messageService.findMessage(projectMessage.getTitle(), pushContent, projectMessage.getUserId(), 1);
            }
            if(projectMessage.getIsEnable() == 1 && projectMessage.getNotEnableReason()!=null){
                pushContent = "项目终止!原因"+projectMessage.getNotEnableReason();
                messageService.findMessage(projectMessage.getTitle(), pushContent, projectMessage.getUserId(), 1);
            }
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(String id){
		ProjectMessage projectMessage = projectMessageService.selectByPrimaryKey(id);
		projectMessage.setContent(projectMessage.getContent().replaceAll("\n", "<br/>").replaceAll("\\s", "&nbsp;"));
		return JSONUtils.serialize(projectMessage);
	}

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			projectMessageService.deleteByPrimaryKey(str);
		}
		return JSONUtils.serialize(new Ret(0));
	}
    @RequestMapping("/update")
    @ResponseBody
    public String update(ProjectMessage projectMessage){
        this.projectMessageService.updateByPrimaryKeySelective(projectMessage);
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/saveConnectMaster")
    @ResponseBody
    public String saveConnectMaster(String  userId,String id){
        if (StringUtils.isNotBlank(userId)){
            int result = projectMessageService.updateConnectMaster(userId,id);
            if(result == 1){
                ProjectMessage projectMessage = projectMessageService.selectByPrimaryKey(id);
                //推送给发布人
                messageService.findMessage(projectMessage.getTitle(), "审核通过！", projectMessage.getUserId(), 2);
                
                //推送给可行性分析人员
                String pushContent2 = "您有一个项目需要进行可行性分析！";
                messageService.findMessage(projectMessage.getTitle(), pushContent2, userId, 2);
            }
            
        }
        return JSONUtils.serialize(new Ret(0));
    }   

    
    @RequestMapping("/detail")
    @ResponseBody
    public String detail(String id){
        ProjectMessageEx projectMessageEx = projectMessageService.selectDetail(id);
        projectMessageEx.setCyTypeId(projectMessageEx.getCyTypeName());
        return JSONUtils.serialize(projectMessageEx);
    }
    
    @RequestMapping("/checkIsEnable")
    @ResponseBody
    public String checkIsEnable(String projectId){
        ProjectMessageEx projectMessageEx = projectMessageService.selectIsEnable(projectId);
        return JSONUtils.serialize(projectMessageEx);
    }
}